package demo.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

// @Controller
@RestController
public class FileController {

	@RequestMapping(value = "/upload")
	public Map<String, Object> upload(@RequestParam("file") MultipartFile file) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String originalFilename = file.getOriginalFilename();
		
		String contentType = file.getContentType();
		
		long size = file.getSize();
		
		map.put("originalFilename", originalFilename);
		map.put("contentType", contentType);
		map.put("size", size);
		
		map.put("info", file.toString());
		map.put("class", file.getClass());
		
		InputStream inputStream = file.getInputStream();

		map.put("inputStreamInfo", inputStream.toString());
		
		if(inputStream instanceof FileInputStream){
			FileInputStream fis = (FileInputStream)inputStream;
			map.put("fileInputStream", fis.getChannel().toString());
			// 获取临时文件路径 ，该文件会被删除 
			// /private/tmp/upload_5a48fb1c_0520_4c12_9cfc_28cfe5cf04b7_00000002.tmp
			Field f = FileInputStream.class.getDeclaredField("path");
			f.setAccessible(true);
			map.put("path", f.get(fis));
		}

		// void transferTo(File dest)
		
		return map;
	}

}